﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Diagnostics;
using Microsoft.Phone.Tasks;
using Theosoft.Mobile.LibService;
using Microsoft.Phone.Notification;
using Microsoft.Phone.Shell;
using System.Text;

namespace TheoFetion
{
    public partial class MainPage : PhoneApplicationPage
    {

        // Constructor
        public MainPage()
        {
            InitializeComponent();

            // Set the data context of the listbox control to the sample data
            DataContext = App.ViewModel;
            this.Loaded += new RoutedEventHandler(MainPage_Loaded);
        }

        // Load data for the ViewModel Items
        private void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            if (!App.ViewModel.IsDataLoaded)
            {
                App.ViewModel.LoadData();
            }
        }

        private void listBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var selected = (ItemViewModel)((ListBox)sender).SelectedItem;
            Debug.WriteLine("You selected order:  {0}", selected.Title);
            this.textBox2.Text = selected.Order;
        }

        private void button3_Click(object sender, RoutedEventArgs e)
        {
            this.textBox2.Text = String.Empty;
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            Microsoft.Phone.Tasks.PhoneNumberChooserTask cellNumber = new Microsoft.Phone.Tasks.PhoneNumberChooserTask();
            cellNumber.Completed += CellNumberChooseTaskCompleted;
            cellNumber.Show();
        }

        void CellNumberChooseTaskCompleted(object sender, PhoneNumberResult e)
        {
            if (e.TaskResult == TaskResult.OK)
            {
                var cellNumber = e.PhoneNumber.Replace("(","").Replace(")","").Replace("-","").Replace("+86","").Replace(" ","");
                this.textBox1.Text = String.IsNullOrEmpty(this.textBox1.Text) ? cellNumber : String.Format("{0},{1}", this.textBox1.Text, cellNumber);
            }
            else
            {
                MessageBox.Show("No cellNumber selected!");
            }
        }

        private void button4_Click(object sender, RoutedEventArgs e)
        {
            this.textBox1.Text = String.Empty;
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            if (String.IsNullOrEmpty(this.textBox1.Text))
            {
                MessageBox.Show("请输入手机号！");
                return;
            }
            if (String.IsNullOrEmpty(this.textBox2.Text))
            {
                MessageBox.Show("请输入内容！");
                return;
            }
            this.progressBar1.IsIndeterminate = true;
            new SendSMSTask() { Completed = ShowSendResult }.SendSMS(this.textBox1.Text, this.textBox2.Text, App.HttpNotifyUrl);
        }

        private void ShowSendResult(object sender, objEventArgs e)
        {
            Debug.WriteLine("Show sendResult.");
            var result = (string)e.msg;
            Dispatcher.BeginInvoke(() =>
            {
                this.progressBar1.IsIndeterminate = false;
                MessageBox.Show("发送成功！");
            });
        }
    }
}